package com.bristua.barcode.supplier.biz;

import com.bristua.barcode.common.exception.BizException;
import com.bristua.barcode.common.util.DateUtils;
import com.bristua.barcode.common.util.SequenceUtil;
import com.bristua.barcode.supplier.dao.TChannelMapper;
import com.bristua.barcode.supplier.enums.SupplierCode;
import com.bristua.barcode.supplier.model.TChannel;
import com.bristua.barcode.supplier.model.condition.TChannelExample;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2019/9/5.
 */
@Service
@Transactional
public class AddChannelBiz {

    @Resource
    private TChannelMapper tChannelMapper;

    public void  addChannel(String channelName,Integer channelType){
        TChannelExample tChannelExample = new TChannelExample();
        tChannelExample.createCriteria().andChannelNameEqualTo(channelName);
        List<TChannel> tChannels = tChannelMapper.selectByExample(tChannelExample);
        if(tChannels!=null && tChannels.size()>0){
            throw new BizException(SupplierCode.CHANNEL_NAME_IS_EXIST.getCode(),SupplierCode.CHANNEL_NAME_IS_EXIST.getMsg());
        }
        TChannel tChannel = new TChannel();
        tChannel.setChannelType(channelType);
        tChannel.setChannelId(SequenceUtil.nextId());
        tChannel.setChannelName(channelName);
        tChannel.setStatus(0);
        tChannel.setCreateTime(DateUtils.getShowDateTime());
        tChannelMapper.insert(tChannel);
    }
}
